package com.ztesoft.sca.thread;

import com.ztesoft.sca.common.BeanFactory;
import com.ztesoft.sca.dao.IomManageDao;
import com.ztesoft.sca.dao.IomManageDaoImpl;
import com.ztesoft.sca.dao.ThreadManageDao;
import com.ztesoft.sca.dao.ThreadManageDaoImpl;
import com.ztesoft.sca.model.AudioSourceDto;
import com.ztesoft.sca.service.IvrConvertService;
import com.ztesoft.sca.service.IvrConvertServiceImpl;
import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Map;

public class IvrConvertRunnable implements Runnable {

	private static Logger logger = Logger.getLogger(IvrConvertRunnable.class);

	private Map orderMap;
    private IvrConvertService ivrConvertService;

	public IvrConvertRunnable(Map order) {
        this.orderMap=order;
	}

	@Override
	public void run() {
        try {
            logger.info(" !!!!进入ConvertRunnable  ");
            long t1=System.currentTimeMillis();
            ivrConvertService = BeanFactory.lookUp(IvrConvertServiceImpl.class);

            if (null != orderMap && !orderMap.isEmpty()) {
                try {
                    //下载
                    orderMap=ivrConvertService.downloadIvrFile(orderMap);
                    long t2=System.currentTimeMillis();
                    logger.info(" 下载完成耗时 "+(t2-t1)+"ms");
                    //转换
                    orderMap=ivrConvertService.convertIvrFile(orderMap);
                    long t3=System.currentTimeMillis();
                    logger.info(" 转换完成耗时 "+(t3-t2)+"ms");
                    //上传
                    orderMap=ivrConvertService.uploadIvrFile(orderMap);
                    long t4=System.currentTimeMillis();
                    logger.info(" 上传完成耗时 "+(t4-t3)+"ms");
                    //记录
                    ivrConvertService.updateIomConvertRecord(orderMap);
                    ivrConvertService.addScaAudioSource(orderMap);
                    long t5=System.currentTimeMillis();
                    logger.info(" 记录完成耗时 "+(t5-t4)+"ms");
                } catch (Exception ex) {
                    logger.error("转换ivr异常," + ex.getMessage(), ex);
                }
            } else {
                logger.debug("转换ivr线程,本次查询无工单");
            }
        } catch (Throwable e) {
            logger.error("转换ivr线程异常," + e.getMessage(), e);
            e.printStackTrace();
        }
	}

}
